<%--
  Created by IntelliJ IDEA.
  User: 陈国钰
  Date: 2016/9/6
  Time: 15:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
    <%@include file="../top.jsp"%>
    <script type="application/javascript">
        var pageNum = parseInt("${page.pageNum}");
        $(function () {
            $("#order").click(function () {
                var roomList = "";
                $("table :checkbox:checked").each(function () {
                    roomList += $(this).siblings(":hidden").val() + ",";
                });
                roomList.substring(0, roomList.length - 1);
                var $parent = $(window.parent.document);
                var beginTime = $parent.find("#beginTime").val();
                var endTime = $parent.find("#endTime").val();
                var price = $("#price").text();
                var hotelId = $("#hotelId").val();
                var url = "${path}/orderAction!booking";
                if (roomList == null) {
                    alert("房间不能为空");
                    return false;
                }
                if (beginTime == null) {
                    alert("入住时间不能为空");
                    return false;
                }
                if (endTime == null) {
                    alert("退订时间不能为空");
                    return false;
                }
                var data = {
                    "order.beginTime": beginTime,
                    "order.endTime": endTime,
                    "order.hotelId": hotelId,
                    "order.orderContext": roomList,
                    "order.price": price,
                    "order.state": 3,
                    "order.userId":${user.userId}
                }
                $.post(url, data, function () {
                    alert("下单成功");
                    var url = "${path}/orderAction!orderList";
                    window.parent.location.href = url;
                }, "json");
            });

            $("table .xuanzhe").live("click", function () {
                var total = 0;
                $("table :checkbox:checked").each(function () {
                    var prices = $(this).closest("tr")
                            .children(".pay")
                            .text();
                    total += parseInt(prices);
                })
                $("#price").html(total);
            })

            $("#more").click(function () {
                var totalPage = parseInt("${page.pageTotal}");
                console.log(pageNum);
                if (pageNum == totalPage) {
                    alert("已结没有更多房间了")
                    return false;
                }
                pageNum = pageNum + 1;
                var url = "${path}/hotelAction!searchRoom?hotelId=" + "${hotelId}" + "&page.pageNum=" + pageNum + "";
                $.getJSON(url, function (jsonData) {
                    console.log(jsonData.pageNum);
                    pageNum = jsonData.pageNum;
                    for (var i = 0; i < jsonData.list.length; i++) {
                        var tr = "<tr>" +
                                "<td>" + '<input type="checkbox" class="xuanzhe"/>' +
                                "<input type='hidden' value='" + jsonData.list[i].roomId + "'/>" +
                                "<input type='hidden' class='hotelId' value='" + jsonData.list[i].hotelId + "'/>" +
                                "</td>" +
                                "<td>" + jsonData.list[i].roomName + "</td>" +
                                "<td>" + jsonData.list[i].roomType + "</td>" +
                                "<td class='pay'>" + jsonData.list[i].roomPay + "</td>" +
                                "<td>" + jsonData.list[i].roomDescription + "</td>" +
                                "<td> <img src=" + jsonData.list[i].roomPicture + "></td>" +
                                "</tr>"
                        $("#last").before(tr);
                    }
                });
            });

        });
    </script>
</head>
<body>
<div>
    <span>总价</span>
    <span id="price">0</span>
    <span><button id="order">下单</button></span>
</div>
<div>
    <table border='1' cellspacing="0" cellpadding="20" style="width: 100%">
        <tr>
            <td>选择房间</td>
            <td>名称</td>
            <td>类型</td>
            <td>价格</td>
            <td>描述</td>
            <th>照片</th>
        </tr>
        <s:iterator value="page.list" var="room">
            <s:if test="#room.roomStatus == 0">
                <tr class="room">
                    <td>
                        <input type="checkbox" class="xuanzhe"/>
                        <input type="hidden" value='<s:property value="#room.roomId"/>'/>
                        <input type="hidden" class="hotelId" value='<s:property value="#room.hotelId"/>'/>
                    </td>
                    <td><s:property value="#room.roomName"/></td>
                    <td><s:property value="#room.roomType"/></td>
                    <td class="pay"><s:property value="#room.roomPay"/></td>
                    <td><s:property value="#room.roomDescription"/></td>
                    <td>
                        <img src="<s:property value="#room.roomPicture"/>" alt=""></td>
                </tr>
            </s:if>
        </s:iterator>
        <tr id="last">
            <td COLSPAN="6">
                <button id="more">点击查看更多房间</button>
            </td>
        </tr>
    </table>
    <input type="hidden" id="hotelId" value="${hotelId}">
</div>
</body>
</html>
